package com.caishi.lkx.exam.service.impl;

import com.caishi.lkx.exam.mapper.MaterialLikeMapper;
import com.caishi.lkx.exam.service.IExColumnConfigReService;
import com.caishi.lkx.exam.service.IMaterialLikeService;
import com.zzw.common.Wrappers;
import com.zzw.common.entity.IBaseMapper;
import com.zzw.common.entity.impl.BSDUAbsEntity;
import com.caishi.lkx.exam.ienum.type.ColumnType;

import com.caishi.lkx.exam.records.RecordsCollectType;

import com.caishi.lkx.user.model.MaterialLikeModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author by liyihua
 * @date : 2022-03-15 10:22
 */
@Service("MaterialLikeServiceImpl")
public class MaterialLikeServiceImpl implements IMaterialLikeService {
    @Resource
    private MaterialLikeMapper mapper;

    @Resource
    private IExColumnConfigReService exColumnConfigReService;

    @Override
    public IBaseMapper<MaterialLikeModel, String> getMapper() {
        return mapper;
    }

    @Override
    public void delUserAllPaperCollect(String userId, Long categoryId) {
        var paperIds = exColumnConfigReService.columnTypeConfigs(categoryId, ColumnType.paperLibrary);
        if (!paperIds.isEmpty()) {
            delete(Wrappers.<MaterialLikeModel>lambdaUpdate().eq(BSDUAbsEntity::getCreatedBy, userId)
                    .in(MaterialLikeModel::getRelationId, paperIds)
                    .eq(MaterialLikeModel::getType, 1)
            );
        }
    }

    @Override
    public void addCollectionQuestions(String userId, String industryCode, String examId, Long categoryId, String relationId, RecordsCollectType recordsType) {
        Long count = this.selectCount(wrappers().eq(MaterialLikeModel::getCreatedBy, userId)
                .eq(MaterialLikeModel::getCategoryId, categoryId)
                .eq(MaterialLikeModel::getRelationId, relationId)
                .eq(MaterialLikeModel::getRecordsType, recordsType)
        );
        if (count > 0) return;
        MaterialLikeModel materialLikeModel = new MaterialLikeModel();
        materialLikeModel.setCategoryId(categoryId);
        materialLikeModel.setPassive(0);
        materialLikeModel.setType(1);
        materialLikeModel.setRecordsType(recordsType);
        materialLikeModel.setRelationId(relationId);
        this.insert(materialLikeModel);
    }

    @Override
    public void delCollectionQuestions(String userId, String industryCode, String examId, Long categoryId, String relationId, RecordsCollectType recordsType) {
        this.delete(Wrappers.<MaterialLikeModel>lambdaUpdate().eq(MaterialLikeModel::getCreatedBy, userId)
                .eq(MaterialLikeModel::getCategoryId, categoryId)
                .eq(MaterialLikeModel::getRelationId, relationId)
                .eq(MaterialLikeModel::getRecordsType, recordsType).eq(MaterialLikeModel::getPassive, 0));
    }
}
